package qiyu;

import java.util.*;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * api测试
 */
public class StreamApi {
    private String[] name = new String[]{"张飞","关羽","猪刚列","孙悟空","刘备","二哥"};
    private Integer[] nums = new Integer[]{1,3,1,7,12,567,12,3453,126,785,123,67,123,546,23,58,76,342,8678,2342,124,85,234,1231,568};
    public List gennerList(){
        List<String> nameList = Arrays.asList(name);
        return nameList;
    }
    public List numberList(){
        List<Integer> list = new ArrayList<>(Arrays.asList(nums));
        return list;

    }

    /**
     * 过滤
     * @param list
     */
    public void filterOne(List list){
        list.stream().filter((item)->{
            if(item.equals("张飞")){
                return true;
            }
            else{
                return false;
            }
        })
                .forEach(System.out::println);
    }
    public void filterTwo(List list){
        list.stream().filter((item)->{
            if(!item.equals("张飞")){
                return true;
            }
            else{
                return false;
            }
        })
                .forEach(System.out::println);
    }

    /**
     * 对每个对象单独处理
     * @param list
     */
    public void  map(List list){
        list.stream().map((item)->{
           item += "天天日婆娘";
            return item;
        })
                .forEach(System.out::println);
    }

    /**
     * 将对象处理后返回数字
     * @param list
     */
    public void mapToInt(List list){
        list.stream().mapToInt((item)->{
            int num = 1;
          switch ((String)item){
              case "张飞":
                  num = 1;
                  break;
              case "关羽":
                 num = 2;
                 break;
                 default:
                     num = 12315;
                     break;
          }
          return num;

        }).forEach(System.out::println);
    }
    public void flatMap(List list) {
            list.stream().flatMap(it->{
                Stream<Object> item = Stream.of(it);
                return item;
            }).forEach(System.out::println);
    }


    /**
     * 去重
     * @param list
     */
    public void distinct(List list){
        list.stream().distinct().forEach(System.out::println);
    }

    /**
     * 自动排序
     * @param list
     */
    public void sorted(List list){
        list.stream().sorted().forEach(System.out::println);
    }

    /**
     * 手动排序
     * @param list
     */
    public void sorted2(List list){
        list.stream().sorted((t1,t2)->{
            return (Integer)t2-(Integer)t1;
        }).forEach(System.out::println);

    }

    /**
     * 处理对象无返回值
     * @param list
     */
    public void peek(List list){
        list.stream().peek((item)->{
            item+="给秦始皇打钱10000块";
            System.out.println(item);
        }).forEach(System.out::println);
    }

    /**
     * 自动排序 反序
     * @param list
     */
    public void sorted3(List list){
        list.stream().sorted(Comparator.reverseOrder()).forEach(System.out::println);
    }

    /**
     * 从头截取
     * @param list
     */
    public void limit(List list){
        list.stream().limit(2).forEach(System.out::println);
    }

    /**
     * 从指定位置开始截取
     * @param list
     */
    public void skip(List list){
        list.stream().skip(3).forEach(System.out::println);
    }
    public void forEachOrdered(List list){
        list.stream().forEachOrdered(System.out::println);

}

public void toArray(List list){
    Object[] objects = list.stream().toArray();
    for(Object s:objects){
        System.out.println(s);
    }

}

    public void reduce(List<Integer> list){

        int result = list.stream().reduce(0,
                (a,b) ->  {
                    System.out.println("=======");
                    System.out.println("a:"+a);
                    System.out.println("b:"+b);
                    System.out.println("==========");
        return     a +  b;
        } );
        System.out.println(result);

    }
    public void reduceTest(List list) {
        Optional accResult = list.stream() .reduce((acc, item) -> {
            System.out.println("acc : " + acc);

            System.out.println("item: " + item);

            System.out.println("--------");
            return acc;
        });
        System.out.println(accResult);
    }
    public void collect(List list){
        List collect =(List) list.stream().sorted().collect(Collectors.toList());
        Iterator iterator = collect.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }

    }
    public void min(List list){
        System.out.println(  list.stream().min((t1,t2)->{
            return (Integer)t1-(Integer)t2;
        }).get());
    }

    /**
     * 统计条数
     * @param list
     */
    public void count(List list){
        long count =  list.stream().count();
        System.out.println(count);
    }

    /**
     * 任意匹配
     * @param list
     */
    public void anyMatch(List list){
        boolean flag = list.stream().anyMatch(item->{
            if(item.equals("张狗狗")){
                return true;
            }
            else{
                return false;
            }
        });

        System.out.println(flag);
    }

    /**
     * 所有都匹配
     * @param list
     */
    public void allMatch(List list){
        boolean flag = list.stream().allMatch(item->{
            if(item.equals("张飞")){
                return true;
            }
            else{
                return false;
            }
        });
        System.out.println(flag);
    }

    public void findFirst(List list){
        Optional first = list.stream().findFirst();
        System.out.println(first);
    }

    private void findAny(List list) {
        Optional any = list.stream().findAny();
        System.out.println(any);
    }
    public void of(List list){
        Stream<String> 张飞 = Stream.of("张飞");
    }
    public static void main(String[] args) {
        StreamApi one = new StreamApi();
        List list = one.gennerList();
        System.out.println("-----filter----");
        one.filterTwo(list);
        System.out.println("=========map==========");
        one.map(list);
        System.out.println("===========maptoint=========");
        one.mapToInt(list);
        System.out.println("===========flatmap=========");
        //one.flatMap(list);
        System.out.println("=======distinct======");
        one.distinct(list);
        System.out.println("======升序==========");
        List numList = one.numberList();
        one.sorted(numList);
        System.out.println("=====降序======");
        one.sorted2(numList);
        System.out.println("======反序======");
        one.sorted3(numList);
        System.out.println("=======peek=========");
        one.peek(list);
        System.out.println("=======limit====");
        one.limit(list);
        System.out.println("==========skip===========");
        one .skip(list);;
        System.out.println("=========foreach ordered======");
        one.forEachOrdered(numList);
        System.out.println("==========to array==========");
        one.toArray(list);
        System.out.println("==========reduce========");

        one.reduce(numList);
        System.out.println("========转成集合========" );
        one.collect(list);
        System.out.println("==========min===");
        one.min(numList);
        System.out.println("===========count========");
        one.count(numList);
        one.count(list);
        System.out.println("=====匹配上某值 ==============");
        one.anyMatch(list);
        System.out.println("=======所有都能匹配========");
        one.allMatch(list);
        System.out.println("======查询首个======");
        one.findFirst(list);
        System.out.println("========查询任意的");
        one.findAny(list);

        System.out.println("==========reduce==========");
        one.reduceTest(list);
        one.reduce(numList);
        System.out.println("=========flatmap========");
        one.flatMap(list);

    }


}


